package controller;

import model.Course;
import service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@Controller
public class CourseController{
    private CourseService courseService;

    public void setCourseService(CourseService courseService){
        this.courseService=courseService;
    }

    @RequestMapping("/getAll_course")
    public ModelAndView getAll() throws Exception{
        ArrayList<Course> course=courseService.getAllCourses();
        ModelAndView modelAndView=new ModelAndView("show_course");
        modelAndView.addObject("Courses",course);
        return modelAndView;
    }

    @RequestMapping("/delete_course")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        String cno= request.getParameter("cno");
        boolean success=courseService.deleteCourse(cno);

        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_course.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_course.jsp";
        }

    }

    @RequestMapping("/get_course")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String cno= request.getParameter("cno");
        ArrayList<Course> course=courseService.getCourse(cno);
        ModelAndView modelAndView=new ModelAndView("show_course1");
        modelAndView.addObject("Course",course);
        return modelAndView;
    }

    @RequestMapping("/edit_course")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String cno= request.getParameter("cno");
        ArrayList<Course> course=courseService.getCourse(cno);
        ModelAndView modelAndView=new ModelAndView("show_course");
        modelAndView.addObject("Course",course);
        return modelAndView;
    }

    @RequestMapping("/doEdit_course")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String cname,String tno,String mn,String cn,RedirectAttributes redirectAttributes) throws Exception{
        String cno= request.getParameter("cno");
        Course course=new Course();
        course.setCno(cno);
        course.setCname(cname);
        course.setTno(tno);
        course.setMn(mn);
        course.setCn(cn);
        boolean succ=courseService.editCourse(course);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_course.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_course.jsp";
        }

    }

    @RequestMapping("/add_course")
    public ModelAndView add() throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_course");
        return modelAndView;

    }

    @RequestMapping("/doAdd_course")
    public String doAdd(String cno,String cname,String tno,String mn,String cn,RedirectAttributes redirectAttributes) throws Exception{
        Course course=new Course();
        course.setCno(cno);
        course.setCname(cname);
        course.setTno(tno);
        course.setMn(mn);
        course.setCn(cn);
        boolean succ=courseService.addCourse(course);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_course.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_course.jsp";
        }
    }
}
